import { _decorator, Component, Label, Node, Sprite } from 'cc';
import { _platform, _player, _resource } from '../Main';
import { PropPanel } from '../panels/PropPanel';
import { PanelName } from '../managers/ResourceManager';
import { PropItem } from '../items/props/PropItem';
import { TT } from '../platforms/minis/TT';
const { ccclass, property } = _decorator;

@ccclass('BaseProp')
export class BaseProp extends Component {

    prop: PropItem = null!

    @property(Label)
    propName: Label = null!

    @property(Sprite)
    propIcon: Sprite = null!

    @property(Label)
    propCount: Label = null!

    start() {
        this.propName.string = this.prop.showName
        this.propIcon.spriteFrame = this.prop.icon
        this.refreshCount()
    }

    refreshCount() {
        let count = _player.getDataNum(this.prop.getIdName(), this.prop.storeAt, 0)
        this.propCount.string = count > 99 ? '99+' : count + ''
    }

    click() {
        if (!this.prop.useable()) {
            return
        }
        let count = _player.getDataNum(this.prop.getIdName(), this.prop.storeAt, 0)
        if (count > 0) {
            this.prop.drop(1, () => {
                this.prop.use()
                this.refreshCount()
            })
        } else {
            let pn: Node = _resource.openPanel(_resource.getPanelPrefab(PanelName.PropPanel))
            let pp: PropPanel = pn.getComponent(PropPanel)
            pp.init(this.prop, this)
        }
    }
}


